//
//  Ball.m
//  rolling
//
//  Created by Mac on 10-7-11.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Ball.h"
#import "RollToll.h"

@implementation Ball
@synthesize location;
@synthesize width;
@synthesize image;
@synthesize speedX;
@synthesize speedY;
@synthesize imageName;

-(id) init{
	self.imageName = @"bird.png";
	self.width = 10;
	self.image = [UIImage imageNamed:self.imageName];
	//TODO: you should deside the difficulty level!
	int difficulty = 1;
	int flag = [RollToll randomInt:4];
	//step1 : location edge?
	//step2 : another x or y?
	//step3 : judge range of $ano then give the speed
	switch (flag) {
		case 0:	//x=0
			location.x = 0;
			location.y = [RollToll randomInt:480];
			if (self.location.y > 240) {
				speedY = (CGFloat)(-1) * [RollToll randomVelocityWith:difficulty]; 
			} else {
				speedY = (CGFloat)(1) * [RollToll randomVelocityWith:difficulty]; 
			}
			self.speedX = (CGFloat)(1) * [RollToll randomVelocityWith:difficulty];
			break;
		case 1:	//x=320
			location.x = 320;
			location.y = [RollToll randomInt:480];
			if (self.location.y > 240) {
				speedY = (CGFloat)(-1) * [RollToll randomVelocityWith:difficulty]; 
			} else {
				speedY = (CGFloat)(1) * [RollToll randomVelocityWith:difficulty]; 
			}
			self.speedX = (CGFloat)(-1) * [RollToll randomVelocityWith:difficulty];
			break;
		case 2:	//y=0
			location.y = 0;
			location.x = [RollToll randomInt:320];
			if (self.location.x > 160) {
				self.speedX = (CGFloat)(-1) * [RollToll randomVelocityWith:difficulty]; 
			} else {
				self.speedX = (CGFloat)(1) * [RollToll randomVelocityWith:difficulty]; 
			}
			self.speedY = (CGFloat)(1) * [RollToll randomVelocityWith:difficulty];
			break;
		case 3:	//y=480
			location.y = 480;
			location.x = [RollToll randomInt:320];
			if (self.location.x > 160) {
				self.speedX = (CGFloat)(-1) * [RollToll randomVelocityWith:difficulty]; 
			} else {
				self.speedX = (CGFloat)(1) * [RollToll randomVelocityWith:difficulty]; 
			}
			self.speedY = (CGFloat)(-1) * [RollToll randomVelocityWith:difficulty];
			break;
		default:
			break;
	}
	
	return self;
}

-(Boolean) isOutOfScreen{
	if (self.location.x < 0.0 || self.location.x > 320.0) {
		return TRUE;
	}
	if (self.location.y < 0.0 || self.location.y > 480.0) {
		return TRUE;
	}
	return FALSE;
}
@end
